﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace _07_动画
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            // 创建线性动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = btn.Width; // 设置动画初始值
            animation.To = btn.Width - 30; // 设置动画目标值
            animation.Duration = TimeSpan.FromMilliseconds(500); // 设置动画持续时间
            animation.AutoReverse = true;   // 设置控件复位
            animation.RepeatBehavior = new RepeatBehavior(5);// RepeatBehavior.Forever; // 设置为无限循环
            animation.Completed += Animation_Completed;
            // 设置执行动画的控件
            btn.BeginAnimation(WidthProperty, animation);
        }

        private void Animation_Completed(object? sender, EventArgs e)
        {
            btn.Content = "执行结束";
        }
    }
}
